﻿(function (window) {
    //资源对象
    var resources = {
        //是否正在加载
        isLoading: false,
        //文件数量
        fileNum: 0,
        //加载完成数量
        loadDoneNum: 0,
        //图像数组
        imgArray: [],
        //动画数组
        animationArray: []
    };

    //创建材质对象
    var texture2D = {};

    //创建动画对象
    var animation = {};

    /*
        加载材质
        参数:
          --名称
          --文件名称
    */
    texture2D.load = function (name, fileName, fun) {
        var img = new Image();
        img.onload = function () {
            //将图片转换为Canvas元素,以便于单个图像的效果处理
            var c = document.createElement("canvas");
            c.width = img.width;
            c.height = img.height;
            var ctx = c.getContext("2d");
            ctx.drawImage(img, 0, 0);
            resources.imgArray.add({ name: name, image: c });
            resources.loadDoneNum++;
            if (fun) fun(this);
            resources.checkLoad();
        };
        img.onerror = function () {
            throw "加载材质对象\"" + name + "\"失败";
            return;
        }
        img.src = fileName;
        resources.fileNum++;
        resources.checkLoad();
    };

    /*
        获取材质尺寸
        参数:
          --名称
    */
    texture2D.getSize = function (name) {
        var img = texture2D.get(name);
        return {
            width: img.width,
            height: img.height
        };
    };

    /*
        获取材质
        参数:
          --名称
    */
    texture2D.get = function (name) {
        var ri = resources.imgArray;
        for (var i = ri.length; i--;) {
            if (name === ri[i].name) {
                return ri[i].image;
            }
        }
    };

    /*
        移除材质
        参数:
          --名称
    */
    texture2D.remove = function (name) {
        var ri = resources.imgArray;
        for (var i = ri.length; i--;) {
            if (name === ri[i].name) {
                resources.imgArray = ri.remove(i);
            }
        }
    };

    /*
        清空材质
    */
    texture2D.clear = function () {
        resources.imgArray.clear();
    };

    /*
        加载动画
        参数:
          --名称
          --文件名称
          --材质名称
          --速度
    */
    animation.load = function (name, fileName, textureName, speed) {
        var file = new window.loadFile(fileName, false, function () {
            resources.animationArray.add({
                isPlayed: false, name: name, action: eval('(' + file.getText() + ')'), actionName: "", speed: speed || 100, curIndex: 0, timer: null, play:
            function (actionName) {
                if (animation.get(name).timer) animation.get(name).stop();
                animation.get(name).curIndex = 0;
                animation.get(name).actionName = actionName;
                animation.get(name).timer = setInterval(function () {
                    animation.get(name).curIndex++;
                    if (animation.get(name).curIndex >= animation.get(name).action[actionName].length) {
                        animation.get(name).curIndex = 0;
                        animation.get(name).isPlayed = true;
                    }
                }, animation.get(name).speed);
            }, stop: function () {
                clearInterval(animation.get(name).timer);
            }, draw: function (x, y, scale) {
                if (!scale) scale = 1;
                var action = animation.get(name);
                var actionRect = action.action[action.actionName][action.curIndex];
                spriteBatch.draw(texture2D.get(textureName), x - actionRect.width / 2, y - actionRect.height / 2, actionRect.width * scale, actionRect.height * scale, actionRect.x, actionRect.y, actionRect.width, actionRect.height);
            }
            });
        });
        file.sendData();
        return animation.get(name);
    };

    /*
        克隆动画
        参数:
          --要克隆的动画名称
          --克隆后的动画名称
    */
    animation.clone = function (name, cloneName) {
        if (!animation.get(cloneName)) {
            resources.animationArray.add(animation.get(name));
            resources.animationArray[resources.animationArray.length - 1].name = cloneName;
            return true;
        }
    };

    /*
        获取动画
        参数:
          --名称
    */
    animation.get = function (name) {
        var ar = resources.animationArray;
        for (var i = ar.length; i--;) {
            if (name === ar[i].name) {
                return ar[i];
            }
        }
    };

    /*
        移除动画
        参数:
          --名称
    */
    animation.remove = function (name) {
        var ar = resources.animationArray;
        for (var i = ar.length; i--;) {
            if (name === ar[i].name) {
                resources.animationArray = ar.remove(i);
            }
        }
    };

    /*
        清空动画
    */
    animation.clear = function () {
        resources.animationArray.clear();
    };

    //将对象公开
    window.resources = resources;
    window.texture2D = texture2D;
    window.animation = animation;
})(window);